﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace dasBlog.Storage.Configuration
{
    public sealed class UserNameCredentialsSection : ConfigurationSection
    {
        // Fields
        private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();
        private static readonly ConfigurationProperty _propUsernameCredentials = new ConfigurationProperty(null, typeof(UserNameCredentialElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);

        // Methods
        static UserNameCredentialsSection()
        {
            _properties.Add(_propUsernameCredentials);
        }

        // Properties
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        [ConfigurationProperty("", IsDefaultCollection = true)]
        public UserNameCredentialElementCollection UserNameCredentials
        {
            get
            {
                return (UserNameCredentialElementCollection)base[_propUsernameCredentials];
            }
        }
    }

    public sealed class UserNameCredentialElementCollection : ConfigurationElementCollection
    {
        // Methods
        public void Add(UserNameCredentialElement element)
        {
            this.BaseAdd(element);
        }

        public void Clear()
        {
            base.BaseClear();
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new UserNameCredentialElement();
        }

        public UserNameCredentialElement Get(string elementKey)
        {
            return (UserNameCredentialElement)base.BaseGet(elementKey);
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UserNameCredentialElement)element).Key;
        }

        public void Remove(UserNameCredentialElement element)
        {
            base.BaseRemove(this.GetElementKey(element));
        }

        // Properties
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        protected override string ElementName
        {
            get
            {
                return "userNameCredential";
            }
        }
    }

    public sealed class UserNameCredentialElement : ConfigurationElement
    {
        // Fields
        private static ConfigurationPropertyCollection _properties =
            new ConfigurationPropertyCollection();
        private static readonly ConfigurationProperty _propName =
            new ConfigurationProperty("name", typeof(string), "",
                ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty _propUsername =
            new ConfigurationProperty("username", typeof(string), "",
                ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty _propPassword =
            new ConfigurationProperty("password", typeof(string), null,
                ConfigurationPropertyOptions.None);

        // Methods
        static UserNameCredentialElement()
        {
            _properties.Add(_propName);
            _properties.Add(_propUsername);
            _properties.Add(_propPassword);
        }

        public UserNameCredentialElement()
        {
        }

        public UserNameCredentialElement(string name, string username, string password)
            : this()
        {
            this.Name = name;
            this.Username = username;
            this.Password = password;
        }

        public override bool Equals(object usernameCredentials)
        {
            UserNameCredentialElement element = usernameCredentials as UserNameCredentialElement;
            if ((element != null) && base.Equals(usernameCredentials))
            {
                return object.Equals(this.Name, element.Name);
            }
            return false;
        }

        public override int GetHashCode()
        {
            return (base.GetHashCode() ^ this.Username.GetHashCode());
        }

        // Properties
        internal string Key
        {
            get
            {
                return this.Name;
            }
        }

        [ConfigurationProperty("name", IsRequired = true, IsKey = true, DefaultValue = "")]
        public string Name
        {
            get
            {
                return (string)base[_propName];
            }
            set
            {
                base[_propName] = value;
            }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        [ConfigurationProperty("username", IsRequired = true, DefaultValue = null)]
        public string Username
        {
            get
            {
                return (string)base[_propUsername];
            }
            set
            {
                base[_propUsername] = value;
            }
        }

        [ConfigurationProperty("password", IsRequired = false, DefaultValue = null)]
        public string Password
        {
            get
            {
                return (string)base[_propPassword];
            }
            set
            {
                base[_propPassword] = value;
            }
        }

    }
}
